$(function () {

  //渲染页面
  var currentPage = 1;
  function render() {
    $.ajax({
      type: "get",
      url: "/user/queryUser",
      data: {
        pageSize: 4,
        page: currentPage
      },
      dataType: "json",
      success: function (res) {
        console.log(res);
        $('tbody').html(template("tmp", res));
        var totalPages = Math.ceil(res.total / 4);
        setPage (totalPages);
      }
    });
  }

  render();

  // 禁用启用按钮
  $("tbody").on("click", ".btn-switch", function () {
    var currentId = $(this).parent().data("id");
    var isDel = +$(this).hasClass("btn-success");
    // console.log(currentId);
    // console.log(isDel);
    $(".btn-ok").off("click").on("click", function () {
      $.ajax({
        type: "post",
        url: "/user/updateUser",
        data: {
          id: currentId,
          isDelete: isDel
        },
        dataType: "json",
        success: function (res) {
          $(".modal-user").modal("hide");
          render();
        }
      });
    })

  })

  //分页器
  function setPage (total) {
    $("#paginator").bootstrapPaginator({
      bootstrapMajorVersion: 3,//默认是2，如果是bootstrap3版本，这个参数必填
      currentPage: currentPage,//当前页
      totalPages: total,//总页数
      onPageClicked: function (event, originalEvent, type, page) {
        //为按钮绑定点击事件 page:当前点击的按钮值
        currentPage = page;
        render();
      }
    });
  }
})